package be.ipl.eBar.servlets;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.ipl.eBar.domaine.Biere;
import be.ipl.eBar.usecases.GestionBieres;

@WebServlet(name="carte.html")
public class ToutesBieres extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	private String carte;

	@EJB
	private GestionBieres gestionBieres;
	
	public void init() {
		List<Biere> bieres = gestionBieres.listerBieres();
		carte = getServletConfig().getInitParameter("carte");
		File file = new File(getServletContext().getRealPath(carte));
		PrintWriter printWriter;
		try {
			printWriter = new PrintWriter(file);
			for(Biere biere : bieres){
				printWriter.println(biere.getNom() + " " + biere.getType()
						+ " " + biere.getCouleur());
			}
			printWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Iterator<Biere> bieres = gestionBieres.listerBieres().iterator();
		request.setAttribute("titre", "Notre Carte");
		request.setAttribute("carte", carte);
		request.setAttribute("liste", bieres);
		RequestDispatcher rd = getServletContext().getNamedDispatcher("Lister");
		rd.forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}